package com.baijiayun.live.ui.utils;

import android.util.Log;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class ColorUtil {
    private static final int lightColorCount = 5;
    private static final int darkColorCount = 4;
    private static final float saturationStep1 = 0.16f;
    private static final float saturationStep2 = 0.05f;
    private static final float brightnessStep1 = 0.05f;
    private static final float brightnessStep2 = 0.15f;
    private static int hueStep = 2;
    private static int i;
    private static String[] colors = new String[]{
            "#f5222d", "#fa541c", "#fa8c16", "#faad14", "#fadb14",
            "#a0d911", "#52c41a", "#13c2c2", "#1890ff", "#2f54eb",
            "#722ed1", "#eb2f96", "#833d41", "#835a3d", "#77833d",
            "#3d8379", "#3d6183", "#3f3d83", "#7f3d83", "#607d8d"
    };

    public static float[] getColor(String userNumber) {
        float[] result = new float[3];
        if (userNumber == null || userNumber.equals("")) {
            return result;
        } else if (userNumber.length() == 1) {
            userNumber = "0" + userNumber;
        }
        String last2Number = userNumber.substring(userNumber.length() - 2);
        int integer;
        try {
            integer = Integer.parseInt(last2Number);
        } catch (Exception e) {
            return result;
        }
        int index = (integer / 10);
        if ((integer % 10) >= 5) {
            index = index + 10;
        }
        String mainColor = colors[index];
        int light = (integer % 5) + 3;
        boolean isLight = light <= 5;
        i = isLight ? lightColorCount - light : light - lightColorCount;
        RGB rgb = getRGBByString(mainColor);
        HSV hsv = rgb.RGB2HSV();
        HSV standardHSV = new HSV(hsv.H * 360, hsv.S, hsv.V);
        HSV deriveHSV = standardHSV.standard2derive(i, isLight);
        result[0] = deriveHSV.H;
        result[1] = deriveHSV.S;
        result[2] = deriveHSV.V;
        return result;
    }


    private static RGB getRGBByString(String mainColor) {
        RGB rgb = new RGB();
        rgb.red = Character.getNumericValue(mainColor.charAt(1)) * 16 + Character.getNumericValue(mainColor.charAt(2));
        rgb.green = Character.getNumericValue(mainColor.charAt(3)) * 16 + Character.getNumericValue(mainColor.charAt(4));
        rgb.blue = Character.getNumericValue(mainColor.charAt(5)) * 16 + Character.getNumericValue(mainColor.charAt(6));
        return rgb;
    }


    static class RGB {
        protected float red;
        protected float green;
        protected float blue;

        public HSV RGB2HSV() {
            this.red = this.red / 255;
            this.blue = this.blue / 255;
            this.green = this.green / 255;
            float max = Math.max(this.red, Math.max(this.blue, this.green));
            float min = Math.min(this.red, Math.min(this.blue, this.green));
            float d = max - min;
            HSV hsv = new HSV();
            hsv.V = max;
            hsv.S = max == 0 ? 0 : d / max;
            if (max == min) {
                hsv.H = 0;
            } else {
                if (max == this.red) {
                    hsv.H = (this.green - this.blue) / d + (this.green < this.blue ? 6 : 0);
                } else if (max == this.blue) {
                    hsv.H = (this.red - this.green) / d + 4;
                } else if (max == this.green) {
                    hsv.H = (this.blue - this.red) / d + 2;
                }
                hsv.H = hsv.H / 6;
            }
            return hsv;
        }
    }

    static class HSV {
        protected float H;
        protected float S;
        protected float V;

        public HSV(float h, float s, float v) {
            H = h;
            S = s;
            V = v;
        }

        public HSV() {
        }

        public HSV standard2derive(int i, boolean isLight) {
            HSV deriveHSV = new HSV();
            if (this.H >= 60 && this.H <= 240) {
                deriveHSV.H = isLight ? this.H - hueStep * i : this.H + hueStep * i;
            } else {
                deriveHSV.H = isLight ? this.H + hueStep * i : this.H - hueStep * i;
            }
            if (deriveHSV.H < 0) {
                deriveHSV.H += 360;
            } else if (deriveHSV.H >= 360) {
                deriveHSV.H -= 360;
            }
            deriveHSV.H = Math.round(deriveHSV.H);

            if (isLight) {
                deriveHSV.S = this.S - saturationStep1 * i;
            } else if (i == darkColorCount) {
                deriveHSV.S = this.S + saturationStep1;
            } else {
                deriveHSV.S = this.S + saturationStep2 * i;
            }
            if (deriveHSV.S > 1) {
                deriveHSV.S = 1;
            }
            if (isLight && i == lightColorCount && deriveHSV.S > 0.1) {
                deriveHSV.S = 0.1f;
            }
            if (deriveHSV.S < 0.06) {
                deriveHSV.S = 0.06f;
            }
            deriveHSV.S = new BigDecimal(deriveHSV.S).setScale(2, RoundingMode.HALF_UP).floatValue();

            if (isLight) {
                deriveHSV.V = this.V + brightnessStep1 * i;
            } else {
                deriveHSV.V = this.V - brightnessStep2 * i;
            }
            if (deriveHSV.V > 1) {
                deriveHSV.V = 1;
            }
            deriveHSV.V = new BigDecimal(deriveHSV.V).setScale(2, RoundingMode.HALF_UP).floatValue();

            return deriveHSV;
        }
    }

}


